This README guides you through replicating the results and analyses from Yue Hu, Yuehong Cassandra Tai, & Frederick Solt's paper, "Revisiting the Evidence on Thermostatic Response to Democratic Change: Degrees of Democratic Support or Researcher Degrees of Freedom?"

Included are instructions for setting up your environment and running the dem_mood.Rmd R Markdown file, which reproduces all results in the main text and supplementary materials.


To ensure smooth compilation, please ensure your environment meets the following software requirements:

-  R version >= 4.3.2,
-  `DCPOtools` 0.1.0.9000,
-  `tinytex` 0.45.

Please make sure you have rmarkdown, bookdown, knitr, and tinytex in your R environment, as these packages are essential for rendering the document through Rmarkdown. Additionally, for replication of measurement procedures, cmdstanr version 0.7.1 is also necessary. 


# Setup
First of all, extract the `renv.zip` in the current directory as a folder of renv/.


Second, please set the working directory to where the dataverse file is located and install basic packages for rendering a file, e.g., 

```r
setwd(~/THE ACTUAL PATH/dataverse_files)
file.create(".here")
# restart R
setwd(~/THE ACTUAL PATH/dataverse_files)
here::here()
current_dir <- getwd()
Sys.setenv(RENV_PROJECT = current_dir)
if(!require(renv)) install.packages("renv")
renv::load()
renv::restore()
```

After creating (".here"), restart R, re-set working directory and check whether here::here() is identical to your working directory. To replicate the file, working directory and root directory for here::here() should be the same. 

After running renv::load(), if you encounter the message "The project located at '~/THE ACTUAL PATH/dataverse_files' has not yet been initialized," please select option 2: 'Continue loading this project as-is.' Then, proceed with renv::restore(). If you receive a notification stating, "- One or more packages recorded in the lockfile are not installed," please restart R and use setwd('~/THE ACTUAL PATH/dataverse_files') to set your working directory again.

Third, before processing the following steps, make sure you have the directory structure as below:

    ~/
    |   
    |   apsr.bst  
    |   dem_mood.Rmd
    |   dem-mood-text.bib
    |   readme.txt
    |   renv.lock
    |   
    |   
    +---data
        |   claassen_input_raw.csv
        |   claassen_input.rds
        |   dem_mood_apsr.csv
        |   df_apsr_list.rds
        |   supdem raw survey marginals.csv
        \---highest
            \---lower
                | supdem.stan.mod5-202402171324-1.csv
                | supdem.stan.mod5-202402171324-2.csv
                | supdem.stan.mod5-202402171324-3.csv
                | supdem.stan.mod5-202402171324.json
	    \---mar
                | supdem.stan.mod5-202402171324-1.csv"
                | supdem.stan.mod5-202402171324-2.csv
                | supdem.stan.mod5-202402171324-3.csv
                | supdem.stan.mod5-202402171324.json
	    \---theory
                | supdem.stan.mod5-202402171324-1.csv
                | supdem.stan.mod5-202402171324-2.csv
                | supdem.stan.mod5-202402171324-3.csv
                | supdem.stan.mod5-202402171324.json
	    \---upper
                | supdem.stan.mod5-202402171324-1.csv
                | supdem.stan.mod5-202402171324-2.csv
                | supdem.stan.mod5-202402171324-3.csv
                | supdem.stan.mod5-202402171324.json
        \---lowest
            \---lower
                | supdem.stan.mod5-202402171404-1.csv
                | supdem.stan.mod5-202402171404-2.csv
                | supdem.stan.mod5-202402171404-3.csv
                | supdem.stan.mod5-202402171404.json
	    \---mar
                | supdem.stan.mod5-202402171404-1.csv
                | supdem.stan.mod5-202402171404-2.csv
                | supdem.stan.mod5-202402171404-3.csv
                | supdem.stan.mod5-202402171404.json
	    \---theory
                | supdem.stan.mod5-202402171404-1.csv
                | supdem.stan.mod5-202402171404-2.csv
                | supdem.stan.mod5-202402171404-3.csv
                | supdem.stan.mod5-202402171404.json
	    \---upper
                | supdem.stan.mod5-202402171404-1.csv
                | supdem.stan.mod5-202402171404-2.csv
                | supdem.stan.mod5-202402171404-3.csv
                | supdem.stan.mod5-202402171404.json
        \---median
            \---lower
                | supdem.stan.mod5-202402171343-1.csv
                | supdem.stan.mod5-202402171343-2.csv
                | supdem.stan.mod5-202402171343-3.csv
                | supdem.stan.mod5-202402171343.json
	    \---mar
                | supdem.stan.mod5-202402171343-1.csv
                | supdem.stan.mod5-202402171343-2.csv
                | supdem.stan.mod5-202402171343-3.csv
                | supdem.stan.mod5-202402171343.json
	    \---theory
                | supdem.stan.mod5-202402171343-1.csv
                | supdem.stan.mod5-202402171343-2.csv
                | supdem.stan.mod5-202402171343-3.csv
                | supdem.stan.mod5-202402171343.json
	    \---upper
                | supdem.stan.mod5-202402171343-1.csv
                | supdem.stan.mod5-202402171343-2.csv
                | supdem.stan.mod5-202402171343-3.csv
                | supdem.stan.mod5-202402171343.json
        \---orig
            \---lower
                | supdem.stan.mod5-202402171306-1.csv
                | supdem.stan.mod5-202402171306-2.csv
                | supdem.stan.mod5-202402171306-3.csv
                | supdem.stan.mod5-202402171306.json
	    \---mar
                | supdem.stan.mod5-202402171306-1.csv
                | supdem.stan.mod5-202402171306-2.csv
                | supdem.stan.mod5-202402171306-3.csv
                | supdem.stan.mod5-202402171306.json
	    \---theory
                | supdem.stan.mod5-202402171306-1.csv
		| supdem.stan.mod5-202402171306-2.csv
                | supdem.stan.mod5-202402171306-3.csv 
                | supdem.stan.mod5-202402171306.json
	    \---upper
                | supdem.stan.mod5-202402171306-1.csv
                | supdem.stan.mod5-202402171306-2.csv
                | supdem.stan.mod5-202402171306-3.csv
                | supdem.stan.mod5-202402171306.json
    |       
    +---R
    |       supdem.stan.mod5.stan
    |       
    |       
    \---renv
        |   .gitignore
        |   activate.R
        |   settings.json
        |   
        \---library
            \*
        
Fourth, based on the above setting, one can render the file through the following command in R: 

```r
rmarkdown::render('dem_mood.Rmd',  encoding = 'UTF-8')

```

Troubleshooting Tips: 
If you receive a warning about the 'Matrix' package, please note it will not affect the replication process. 
For issues related to DCPOtools installation (e.g. with_min_yrs()), ensure the 'loo','posterior','rstan' and 'BiocManager' packages are installed beforehand.



# Replicating the results in the manuscript and supplementary materials
 
`dem_mood.Rmd` requires the following files to produce results:


- Files to compile the PDF (saving them at the same directory as the rmd file)
    - dem_mood_text.bib
    - apsr.bst

- data/
    - claassen_input_raw.csv
    - claassen_input.rds
    - dem_mood_apsr.csv
    - df_apsr_list.rds
    - supdem raw survey marginals.csv


# Recreating source files and the 16 versions of measures

We have included code that allows for the automatic download of source files and measures via the `dataverse` or `osfr` package. However, obtaining an API key from the dataverse or OSF website is a prerequisite.

To ensure full transparency in our analysis, we also provide codes that enable the recreation of measures based on 16 different choices, located in the data/ directory. It's important to note that going through these steps isn't necessary for compiling the Rmd file or producing the figures and tables presented in the paper.

For readers interested in recreating the 16 measure versions stored in the data/highest, data/lowest, data/median, and data/orig folders, the file R/supdem.stan.mod5.stan is essential. Replication can be achieved by removing eval = FALSE. Please be advised that regenerating these 16 versions of measures is time-consuming.

	